import { createRouter, createWebHistory } from 'vue-router'

import routes from './routers'
import { get_token } from '@/utils/auth';
import { check_login } from '@/api/login';
import { close_loading, start_loading } from '@/utils/loading';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

//导航守卫
router.beforeEach(async (to, from, next) => {
  document.title = to.meta.title;

  start_loading();
  if (!to.meta.not_check) {
    if (!get_token()) {
      next('login');
      return;
    }
    //验证当前登录信息是否过期
    const res = await check_login();
    // console.log(res);
    //什么样的系统 编写什么规格的代码
    // console.log(res,"---------");
    sessionStorage.setItem("curr_user", JSON.stringify(res.data));
  }
  next();
});

router.afterEach(() => {
  close_loading();
})

export default router
